package com.reservation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reservation.common.BusinessException;
import com.reservation.entity.Announcement;
import com.reservation.mapper.AnnouncementMapper;
import com.reservation.service.AnnouncementService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 公告服务实现类
 */
@Service
public class AnnouncementServiceImpl implements AnnouncementService {

    @Autowired
    private AnnouncementMapper announcementMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Announcement addAnnouncement(Announcement announcement) {
        // 设置发布时间、创建时间和更新时间
        if (announcement.getPublishTime() == null) {
            announcement.setPublishTime(LocalDateTime.now());
        }
        announcement.setCreateTime(LocalDateTime.now());
        announcement.setUpdateTime(LocalDateTime.now());

        // 保存公告信息
        announcementMapper.insert(announcement);

        return announcement;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Announcement updateAnnouncement(Announcement announcement) {
        // 检查公告是否存在
        Announcement existAnnouncement = announcementMapper.selectById(announcement.getId());
        if (existAnnouncement == null) {
            throw new BusinessException("公告不存在");
        }

        // 设置更新时间
        announcement.setUpdateTime(LocalDateTime.now());

        // 更新公告信息
        announcementMapper.updateById(announcement);

        // 返回更新后的公告信息
        return announcementMapper.selectById(announcement.getId());
    }

    @Override
    public Announcement getAnnouncementById(Long id) {
        Announcement announcement = announcementMapper.selectById(id);
        if (announcement == null) {
            throw new BusinessException("公告不存在");
        }
        return announcement;
    }

    @Override
    public boolean updateStatus(Long id) {
        return announcementMapper.updateStatus(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteAnnouncement(Long id) {
        // 检查公告是否存在
        Announcement announcement = announcementMapper.selectById(id);
        if (announcement == null) {
            throw new BusinessException("公告不存在");
        }

        // 删除公告
        return announcementMapper.deleteById(id) > 0;
    }

    @Override
    public IPage<Announcement> getAnnouncementList(Page<Announcement> page, String title, String status) {
        LambdaQueryWrapper<Announcement> queryWrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        if (StringUtils.isNotBlank(title)) {
            queryWrapper.like(Announcement::getTitle, title);
        }

        if (StringUtils.isNotBlank(status)) {
            queryWrapper.eq(Announcement::getStatus, status);
        }


        // 按发布时间降序排序
        queryWrapper.orderByDesc(Announcement::getPublishTime);

        // 执行查询
        return announcementMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<Announcement> getAllAnnouncements() {
        LambdaQueryWrapper<Announcement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Announcement::getPublishTime);
        return announcementMapper.selectList(queryWrapper);
    }

    @Override
    public List<Announcement> getLatestAnnouncements(int limit) {
        LambdaQueryWrapper<Announcement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Announcement::getPublishTime);

        // 分页查询，只取前limit条
        Page<Announcement> page = new Page<>(1, limit);
        IPage<Announcement> announcementPage = announcementMapper.selectPage(page, queryWrapper);

        return announcementPage.getRecords();
    }
}